package com.mobartisan.vehiclenetstore.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.mobartisan.vehiclenetstore.R;
import com.mobartisan.vehiclenetstore.ui.adapter.GuidanceAdapter;
import com.mobartisan.vehiclenetstore.ui.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by L on 2017/1/20.
 */

public class GuidanceActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    ViewPager viewPager = null;
    List<View> list = null;
    GuidanceAdapter guidanceAdapter= null;
    LinearLayout points = null;
    private ImageView[] tips = new ImageView[3];
    View view1,view2,view3,view4;
    TextView textView1,textView2,textView3;
    Button button ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guidance);
        initView();
        initPager();
    }

    @Override
    public void initData() {

    }



    private void initPager() {
        list= new ArrayList<>();
        view1 = LayoutInflater.from(this).inflate(R.layout.item_welcome,null);
        list.add(view1);
        view2 = LayoutInflater.from(this).inflate(R.layout.item_welcome2,null);
        list.add(view2);
        view4 = LayoutInflater.from(this).inflate(R.layout.item_welcome4,null);
        list.add(view4);
        inittext();
        initGuidanceImg();

        guidanceAdapter = new GuidanceAdapter(list);
        viewPager.setAdapter(guidanceAdapter);
        viewPager.setOnPageChangeListener(this);
    }

    private void inittext() {
        textView1 = (TextView)view1.findViewById(R.id.skipId1);
        textView2 = (TextView)view2.findViewById(R.id.skipId2);
        button = (Button) view4.findViewById(R.id.buttonId);
        textView1.setOnClickListener(this);
        textView2.setOnClickListener(this);
        button.setOnClickListener(this);
    }
    @Override
    public void initView() {
        viewPager= (ViewPager) findViewById(R.id.WelComeId);
        points  = (LinearLayout) findViewById(R.id.points);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position != 2) {
            points.setVisibility(View.VISIBLE);
            setImageBackground(position % tips.length);
        } else {
            points.setVisibility(View.GONE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    private void initGuidanceImg() {
        LinearLayout.LayoutParams pointsParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        pointsParams.leftMargin = 5;
        pointsParams.rightMargin = 5;
        points.removeAllViews();

        for (int i = 0; i < list.size(); i++) {
            ImageView point = new ImageView(this);
            point.setLayoutParams(new LinearLayout.LayoutParams(7, 7));
            tips[i] = point;
            if (i == 0) {
                tips[i].setBackgroundResource(R.drawable.chicked);
            } else {
                tips[i].setBackgroundResource(R.drawable.unchick);
            }
            points.addView(point, pointsParams);
        }
    }
    private void setImageBackground(int selectItems) {
        for (int i = 0; i < tips.length; i++) {
            if (i == selectItems) {
                tips[i].setBackgroundResource(R.drawable.chicked);
            } else {
                tips[i].setBackgroundResource(R.drawable.unchick);
            }

        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.skipId1:
                getCallingIntent(GuidanceActivity.this);
                this.finish();
                break;
            case R.id.skipId2:
                getCallingIntent(GuidanceActivity.this);
                this.finish();
                break;
            case R.id.buttonId:
                getCallingIntent(GuidanceActivity.this);
                this.finish();
                break;
        }
    }

    public static void getCallingIntent(Context context) {
        Intent callingIntent = new Intent(context, MainActivity.class);
        context.startActivity(callingIntent);
    }

    @Override
    public Activity getActivity() {
        return this;
    }
}
